<?
require_once('ConverterBase.php');

class EntityConverter extends ConverterBase {
	public function setClass($className) {
		$this->className= $className;
		return $this;
	}
	protected function toDatatype($string) {
		if (empty($string)) return $this->getDefault();
		try {
			$entity= new $this->className();
			return $entity->getByPK($string);
		}
		catch (Exception $e) {
			throw new ConverterException($this,'Convertion from "'.$string.'" to "'.$this->className.'" failed.');
		}
	}
	protected function toString($data) {
		return (string)$data->id;
	}
}
